package com.aiju.common;

import com.aiju.base.activity.AbsActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author: 佐为
 * @date: 2016-11-08 14:10
 * @version: V1.0
 * @Description: Activity 管理栈
 */
public class ActivityStack {

    private static Stack<AbsActivity> activites = new Stack<AbsActivity>();
    private static int stubIdx = Integer.MAX_VALUE;

    private ActivityStack() {
    }

    /**
     * 清除顶部登录界面，或者其他
     */
    public static void cleanTops(int size) {
        ArrayList<AbsActivity> list = getActivityList();
        if (list != null && list.size() > 0) {
            List<AbsActivity> absActivities = list.subList(size, list.size());
            for (AbsActivity a : absActivities) {
                a.finish();
            }
        }
    }


    /**
     * 关闭AbsActivity，并从堆栈中移除
     */
    public static void finish(AbsActivity activity) {
        if (activity != null) {
            activity.finish();
            // 从堆栈中移除
            activites.remove(activity);
            activity = null;
        }
    }

    /**
     * 获取的栈顶的AbsActivity
     */
    public static AbsActivity getTopActivity() {
        if (!activites.isEmpty()) {
            return activites.pop();
        }
        return null;
    }

    /**
     * 获取的栈底的AbsActivity
     */
    public static AbsActivity getLastActivity() {
        if (!activites.isEmpty()) {
            return activites.lastElement();
        }
        return null;
    }

    /**
     * 移除栈顶的AbsActivity
     */
    public synchronized static void pop(AbsActivity activity) {
        if (activity != null && activites.size() > 0) {
            activites.remove(activity);
        }
    }

    /**
     * 返回栈顶的第几个AbsActivity(不移除)
     */
    public static AbsActivity getLastActivityAt(int index) {
        if (index > 0 && index < activites.size()) {
            return activites.elementAt(activites.size() - index);
        }
        return null;
    }

    public static AbsActivity findActivity(String clazz) {
        if (clazz == null) {
            return null;
        }
        for (int i = activites.size() - 1; i >= 0; i--) {
            AbsActivity AbsActivity = activites.elementAt(i);
            if (clazz.equals((AbsActivity).getClass().getName())) {
                return AbsActivity;
            }
        }
        return null;
    }

    /**
     * 往栈中压入AbsActivity
     */
    public static void push(AbsActivity activity) {
        if (activity != null && null != activites) {
            activites.push(activity);
        }
    }

    /**
     * 销毁栈的AbsActivity,并清空栈
     */
    public static void finishAll() {
        while (!activites.isEmpty()) {
            finish(getTopActivity());
        }
        activites.clear();
    }

    /**
     * 销毁栈顶的若干AbsActivity
     */
    public static void finishStubTop() {
        while (size() > stubIdx) {
            finish(getTopActivity());
        }
        stubIdx = Integer.MAX_VALUE;
    }

    public static void tagStub() {
        stubIdx = size();
    }

    /**
     * 获取栈的大小
     */
    public static int size() {
        if (null != activites) {
            return activites.size();
        }
        return 0;
    }

    /**
     * 销毁栈顶的若干AbsActivity
     */
    public static void cleanTop(int size) {
        for (int i = 0; i < size; i++) {
            finish(activites.lastElement());
        }
    }

    public static ArrayList<AbsActivity> getActivityList() {
        return new ArrayList<AbsActivity>(activites);
    }
}
